﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Forms
{
	public partial class FormChangeDefaultLogMethod : Form
	{
		public FormChangeDefaultLogMethod(Repository.Verb verb,
			Repository.LogicalWebMethod oldLogMethod, Repository.LogicalWebMethod newLogMethod)
		{
			if ( verb == null ) throw new ArgumentNullException("verb");

			_newLogicalMethod = newLogMethod;

			InitializeComponent();

			this.DialogResult = DialogResult.Cancel;

			dgvChangable.AutoGenerateColumns = false;

			lblInfo.Text = string.Format(lblInfo.Text, verb.Name);

			BindingListEx<Repository.LogicalActivity> logacts =
				Global.Data.GetLogicalActivitiesOfVerb(verb.ID);

			_items = new BindingListEx<ChangeListItem>();
			foreach ( var item in logacts ) {
				if ( item.LogicalMethod == oldLogMethod ) {
					_items.Add(new ChangeListItem(item));
				}
			}
			dgvChangable.DataSource = _items;
		}

		private BindingListEx<ChangeListItem> _items;
		private Repository.LogicalWebMethod _newLogicalMethod; 

		private class ChangeListItem
		{
			public ChangeListItem(Repository.LogicalActivity logact)
			{
				this.LogicalActivity = logact;
				this.Verb = logact.Verb;
				this.Workflow = logact.Workflow;

				this.Selected = false;
			}

			public bool Selected { get; set; }
			public Image Image { get { return this.Verb != null ? this.Verb.Image : this.LogicalActivity.Image; } }
			public string Name { get { return this.LogicalActivity.Name; } }
			public string WorkflowName { get { return this.Workflow == null ? "" : this.Workflow.Name; } }

			public Repository.Verb Verb { get; protected set; }
			public Repository.LogicalActivity LogicalActivity { get; protected set; }
			public Repository.Workflow Workflow { get; protected set; }

		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			foreach ( var item in _items ) {
				if ( item.Selected ) {
					item.LogicalActivity.LogicalMethod = _newLogicalMethod;
					item.LogicalActivity.Save();
				}
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void dgvChangable_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 && e.ColumnIndex != colSelect.Index ) return;

			ChangeListItem item = dgvChangable.Rows[e.RowIndex].DataBoundItem as ChangeListItem;
			if ( item == null ) return;

			item.Selected = !item.Selected;

			dgvChangable.UpdateCellValue(e.ColumnIndex, e.RowIndex);
		}

		private void tsbtnSelectAll_Click(object sender, EventArgs e)
		{
			foreach ( var item in _items ) {
				item.Selected = true;
			}
			dgvChangable.Refresh();
		}

		private void tsbtnDeselectAll_Click(object sender, EventArgs e)
		{
			foreach ( var item in _items ) {
				item.Selected = false;
			}
			dgvChangable.Refresh();
		}
	}
}
